using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;
namespace Impact.Model.Objects
{
	/// <summary>
	/// Strongly-typed collection for the PageType class.
	/// </summary>
    [Serializable]
	public partial class PageTypeCollection : ActiveList<PageType, PageTypeCollection>
	{	   
		public PageTypeCollection() {}
	}
	/// <summary>
	/// This is an ActiveRecord class which wraps the impact_PageTypes table.
	/// </summary>
	[Serializable]
	public partial class PageType : ActiveRecord<PageType>, IActiveRecord
	{
		#region .ctors and Default Settings
		
		public PageType()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}
		
		private void InitSetDefaults() { SetDefaults(); }
		
		public PageType(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}
        
		public PageType(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}
		 
		public PageType(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}
		
		protected static void SetSQLProps() { GetTableSchema(); }
		
		#endregion
		
		#region Schema and Query Accessor	
		public static Query CreateQuery() { return new Query(Schema); }
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}
		}
		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("impact_PageTypes", TableType.Table, DataService.GetInstance("ImpactProvider"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarTypeId = new TableSchema.TableColumn(schema);
				colvarTypeId.ColumnName = "TypeId";
				colvarTypeId.DataType = DbType.Int32;
				colvarTypeId.MaxLength = 0;
				colvarTypeId.AutoIncrement = true;
				colvarTypeId.IsNullable = false;
				colvarTypeId.IsPrimaryKey = true;
				colvarTypeId.IsForeignKey = false;
				colvarTypeId.IsReadOnly = false;
				colvarTypeId.DefaultSetting = @"";
				colvarTypeId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarTypeId);
				
				TableSchema.TableColumn colvarTypeName = new TableSchema.TableColumn(schema);
				colvarTypeName.ColumnName = "TypeName";
				colvarTypeName.DataType = DbType.AnsiString;
				colvarTypeName.MaxLength = 50;
				colvarTypeName.AutoIncrement = false;
				colvarTypeName.IsNullable = false;
				colvarTypeName.IsPrimaryKey = false;
				colvarTypeName.IsForeignKey = false;
				colvarTypeName.IsReadOnly = false;
				colvarTypeName.DefaultSetting = @"";
				colvarTypeName.ForeignKeyTableName = "";
				schema.Columns.Add(colvarTypeName);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["ImpactProvider"].AddSchema("impact_PageTypes",schema);
			}
		}
		#endregion
		
		#region Props
		  
		[XmlAttribute("TypeId")]
		[Bindable(true)]
		public int TypeId 
		{
			get { return GetColumnValue<int>(Columns.TypeId); }
			set { SetColumnValue(Columns.TypeId, value); }
		}
		  
		[XmlAttribute("TypeName")]
		[Bindable(true)]
		public string TypeName 
		{
			get { return GetColumnValue<string>(Columns.TypeName); }
			set { SetColumnValue(Columns.TypeName, value); }
		}
		
		#endregion
		
		
		#region PrimaryKey Methods		
		
        protected override void SetPrimaryKey(object oValue)
        {
            base.SetPrimaryKey(oValue);
            
            SetPKValues();
        }
        
				
		private Impact.Model.Objects.ImpactPageCollection colImpactPages;
		public Impact.Model.Objects.ImpactPageCollection ImpactPages
		{
			get
			{
				if(colImpactPages == null)
				{
					colImpactPages = new Impact.Model.Objects.ImpactPageCollection().Where(ImpactPage.Columns.Type, TypeId).Load();
					colImpactPages.ListChanged += new ListChangedEventHandler(colImpactPages_ListChanged);
				}
				return colImpactPages;			
			}
			set 
			{ 
					colImpactPages = value; 
					colImpactPages.ListChanged += new ListChangedEventHandler(colImpactPages_ListChanged);
			}
		}
		
		void colImpactPages_ListChanged(object sender, ListChangedEventArgs e)
		{
		    if (e.ListChangedType == ListChangedType.ItemAdded)
		    {
		        // Set foreign key value
		        colImpactPages[e.NewIndex].Type = TypeId;
		    }
		}
		
		#endregion
		
			
		
		//no foreign key tables defined (0)
		
		
		
		//no ManyToMany tables defined (0)
		
        
        
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(string varTypeName)
		{
			PageType item = new PageType();
			
			item.TypeName = varTypeName;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(int varTypeId,string varTypeName)
		{
			PageType item = new PageType();
			
				item.TypeId = varTypeId;
			
				item.TypeName = varTypeName;
			
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		#endregion
        
        
        
        #region Typed Columns
        
        
        public static TableSchema.TableColumn TypeIdColumn
        {
            get { return Schema.Columns[0]; }
        }
        
        
        
        public static TableSchema.TableColumn TypeNameColumn
        {
            get { return Schema.Columns[1]; }
        }
        
        
        
        #endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string TypeId = @"TypeId";
			 public static string TypeName = @"TypeName";
						
		}
		#endregion
		
		#region Update PK Collections
		
        public void SetPKValues()
        {
                if (colImpactPages != null)
                {
                    foreach (Impact.Model.Objects.ImpactPage item in colImpactPages)
                    {
                        if (item.Type != TypeId)
                        {
                            item.Type = TypeId;
                        }
                    }
               }
		}
        #endregion
    
        #region Deep Save
		
        public void DeepSave()
        {
            Save();
            
                if (colImpactPages != null)
                {
                    colImpactPages.SaveAll();
               }
		}
        #endregion
	}
}
